<template>
  <div class="login">
    <el-form ref="loginForm" :model="loginForm" class="login-form">
      <h3 class="title">XXX电商后台系统</h3>
      <div class="info">{{infoMsg}}</div>
      <el-form-item>
        <el-input
            v-model="loginForm.username"
            type="text"
            auto-complete="off"
            placeholder="账号"
          >
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-input
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            placeholder="密码"
        >
        </el-input>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button
            type="primary"
            style="width:100%;"
            @click.native.prevent="handleLogin"
            >登 录
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

import axios from 'axios'

import md5 from 'md5'

export default {
  name: "Login",
  data(){
    return {
      infoMsg: '',
      loginForm: {
        username: 'admin',
        password: "123"
      }
    }
  },
  methods: {
    async handleLogin(){
      console.log("登录事件");
      // console.log(md5("123"));
      // 发送异步请求
      const url = "http://localhost:8888/admin/user/login";
      // axios.post(url, this.loginForm).then(ret => {
      //   console.log(ret);
      //   // 判断登录状态
      // })
      console.log(this.loginForm.password)
      console.log(md5(this.loginForm.password))
      const {data: ret} = await axios.post(url, {
        username: this.loginForm.username,
        password: md5(this.loginForm.password)
      });
      console.log(ret);
      if (ret['code'] == 200) {
        console.log(ret['msg']);
        this.infoMsg = '';
        // 需要将这里暂时用户名存到sessionStorage，记录登录状态
        // sessionStorage.setItem("token", ret['data']['username']);
        sessionStorage.setItem("token", ret['data']['token']);
        // 跳转到系统的首页
        this.$router.replace('/index');
      } else {
        // 登录失败
        this.infoMsg = ret['msg'];
      }
    }
  }
}
</script>

<style scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-color: #2b4b6b;
  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}
.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
}
.login-form .el-input {
  height: 38px;
}
.login-form .el-input input {
  height: 38px;
}
.info {
  color: red;
  text-align: center;
}
</style>